package com.moxiao.yfjh.module.maker.controller.admin.memberStringing;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.maker.common.GenMakerNoCommonUtil;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExcelVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingRespVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingUpdateReqVO;
import com.moxiao.yfjh.module.maker.convert.memberStringing.MatchmakerMemberStringingConvert;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.service.matchMakerStringing.MatchmakerStringingService;
import com.moxiao.yfjh.module.maker.service.matchmakerInfo.MatchmakerInfoService;
import com.moxiao.yfjh.module.maker.service.memberStringing.MatchmakerMemberStringingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "管理后台 - 相亲红娘会员用户牵线总次数")
@RestController
@RequestMapping("/maker/matchmaker-member-stringing")
@Validated
public class MatchmakerMemberStringingController {

    @Resource
    private MatchmakerMemberStringingService matchmakerMemberStringingService;
    @Resource
    private GenMakerNoCommonUtil genMakerNoCommonUtil;

    @Resource
    private BlindMemberUserService blindMemberUserService;

    @Resource
    private MatchmakerStringingService matchmakerStringingService;

    @Resource
    private MatchmakerInfoService matchmakerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲红娘会员用户牵线总次数")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:create')")
    public CommonResult<Long> createMatchmakerMemberStringing(@Valid @RequestBody MatchmakerMemberStringingCreateReqVO createReqVO) {
        genMakerNoCommonUtil.addMemberStringingNo(createReqVO);
        return success(matchmakerMemberStringingService.createMatchmakerMemberStringing(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲红娘会员用户牵线总次数")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:update')")
    public CommonResult<Boolean> updateMatchmakerMemberStringing(@Valid @RequestBody MatchmakerMemberStringingUpdateReqVO updateReqVO) {
        matchmakerMemberStringingService.updateMatchmakerMemberStringing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲红娘会员用户牵线总次数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:delete')")
    public CommonResult<Boolean> deleteMatchmakerMemberStringing(@RequestParam("id") Long id) {
        matchmakerMemberStringingService.deleteMatchmakerMemberStringing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲红娘会员用户牵线总次数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:query')")
    public CommonResult<MatchmakerMemberStringingRespVO> getMatchmakerMemberStringing(@RequestParam("id") Long id) {
        MatchmakerMemberStringingDO matchmakerMemberStringing = matchmakerMemberStringingService.getMatchmakerMemberStringing(id);
        return success(MatchmakerMemberStringingConvert.INSTANCE.convert(matchmakerMemberStringing));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲红娘会员用户牵线总次数列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:query')")
    public CommonResult<List<MatchmakerMemberStringingRespVO>> getMatchmakerMemberStringingList(@RequestParam("ids") Collection<Long> ids) {
        List<MatchmakerMemberStringingDO> list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(ids);
        return success(MatchmakerMemberStringingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲红娘会员用户牵线总次数分页")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:query')")
    public CommonResult<PageResult<MatchmakerMemberStringingRespVO>> getMatchmakerMemberStringingPage(@Valid MatchmakerMemberStringingPageReqVO pageVO) {
        PageResult<MatchmakerMemberStringingDO> pageResult = matchmakerMemberStringingService.getMatchmakerMemberStringingPage(pageVO);
        PageResult<MatchmakerMemberStringingRespVO> voPageResult = MatchmakerMemberStringingConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(voPageResult)){
            List<MatchmakerMemberStringingRespVO> list = voPageResult.getList();
            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(matchmakerMemberStringingRespVO -> {
                    Long memberId = matchmakerMemberStringingRespVO.getMemberId();
                    BlindMemberUserDO memberUser = blindMemberUserService.getMemberUser(memberId);
                    if (Objects.nonNull(memberUser)){
                        matchmakerMemberStringingRespVO.setMobile(memberUser.getMobile());
                    }
                    MatchmakerInfoDO matchmakerInfo = matchmakerInfoService.getMatchmakerInfo(matchmakerMemberStringingRespVO.getMatchmakerId());
                    if (Objects.nonNull(matchmakerInfo)){
                        matchmakerMemberStringingRespVO.setMatchmakerName(matchmakerInfo.getName());
                    }
                    MatchmakerStringingExportReqVO exportReqVO = new MatchmakerStringingExportReqVO();
                    exportReqVO.setActiveId(matchmakerMemberStringingRespVO.getMemberId());
                    List<MatchmakerStringingDO> matchmakerStringingList = matchmakerStringingService.getMatchmakerStringingList(exportReqVO);
                    matchmakerMemberStringingRespVO.setStringingResidueNum(matchmakerMemberStringingRespVO.getStringingNum() - matchmakerStringingList.size());
                });
            }
        }
        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲红娘会员用户牵线总次数 Excel")
    @PreAuthorize("@ss.hasPermission('maker:matchmaker-member-stringing:export')")
    @OperateLog(type = EXPORT)
    public void exportMatchmakerMemberStringingExcel(@Valid MatchmakerMemberStringingExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MatchmakerMemberStringingDO> list = matchmakerMemberStringingService.getMatchmakerMemberStringingList(exportReqVO);
        // 导出 Excel
        List<MatchmakerMemberStringingExcelVO> datas = MatchmakerMemberStringingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲红娘会员用户牵线总次数.xls", "数据", MatchmakerMemberStringingExcelVO.class, datas);
    }

}
